package com.qianyu.api;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import com.qianyu.exception.CustomerException;
import com.qianyu.util.HttpKit;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ApiCommon {
    private final String baseUrl = "http://121.196.203.170:9001";

    //请求申请url
    private final String applyQqUrl = "http://118.178.125.179:7203/raise/account/lend";
    //长效代理url
    private final String proxyUrl = "http://114.55.52.42:9001/ros/api/getComIp";

    //刷新缓存
    private final String refreshCacheUrl = "http://118.178.125.179:7203/v1/account/cache";

    //查询信用分
    private final String queryCommonCreditUrl = baseUrl + "/api/auth/activateCache/queryCommonCredit";
    //查询Q币
    private final String fetchCachedQbQuantityUrl = baseUrl + "/api/auth/activateCache/fetchCachedQbQuantity";
    //获取活动列表
    private final String fetchQBActivityListUrl = baseUrl + "/api/qq/game/fetchQBActivityList";

    //领取qb
    private final String claimQBActivityUrl = baseUrl + "/api/qq/game/claimQBActivity";


    private final String redMachineListUrl = "http://112.124.8.250:7202/openapi/shu/red_machine_list";


    /**
     * 申请qq（hjb接口）
     */
    public String applyQq(Integer num) {
        Map<String, String> map = new HashMap<>();
        map.put("app", "redEnvelope");
        map.put("num", String.valueOf(num));
        map.put("cache", "1");
        try {
            return HttpKit.post(applyQqUrl, map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 获取云机
     * @param groupId
     * @return
     */
    public String redMachineList(Integer groupId){
        Map<String, String> map = new HashMap<>();
        map.put("group_id", String.valueOf(groupId));
        try {
            return HttpKit.get(redMachineListUrl, map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 刷新缓存
     */
    public String refreshCache(String qq, Integer index) {
        Map<String, String> map = new HashMap<>();
        map.put("from", "coin");
        map.put("type", String.valueOf(1));
        map.put("account", qq);
        try {
            if (index < 3) {
                String json = HttpKit.get(refreshCacheUrl, map);
                JSONObject jsonObject = JSONObject.parseObject(json);
                if (jsonObject.getInteger("code") == 0) {
                    return jsonObject.getJSONObject("data").getString("cache");
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 获取活动列表
     */
    public JSONObject fetchQBActivityList(String url) {
        Map<String, String> map = new HashMap<>();
        map.put("gameActivityUrl", url);
        try {
            String body = HttpRequest.post(fetchQBActivityListUrl).body(JSONObject.toJSONString(map)).execute().body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            if (jsonObject.getInteger("code") != 200) {
                throw new CustomerException("活动链接有误！");
            }
            return jsonObject.getJSONObject("data");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
